package com.foreveross.fetl.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.foreveross.fetl.common.vo.model.Agent;
import com.foreveross.fetl.common.vo.model.Cabin;
import com.foreveross.fetl.common.vo.model.SinglePlaneInfo;
import com.foreveross.fetl.common.vo.model.StopOver;
import com.foreveross.fetl.common.vo.model.Transit;
import com.foreveross.fetl.common.vo.model.doub.CabinRelation;
import com.foreveross.fetl.common.vo.model.doub.DoublePlaneInfo;
import com.foreveross.fetl.common.vo.model.doub.ReturnAgent;
import com.foreveross.fetl.common.vo.model.doub.ReturnCabin;
import com.foreveross.fetl.common.vo.model.doub.ReturnDoublePlaneInfo;
import com.foreveross.fetl.common.vo.model.doub.ReturnStopOver;
import com.foreveross.fetl.common.vo.model.doub.ReturnTransit; 

public class HbaseToPlaneInfoUtil {
	
	
	/**
	 * 将map集合转换为 SinglePlaneInfo 对象
	 * @param fcv Map<String, Map<String, String>>
	 * @param refePrice 汇率
	 * @return
	 */
	public static <T extends SinglePlaneInfo> T hbaseToSinglePlaneInfo(Map<String, Map<String, String>> fcv,double refePrice,Class<T> clz){
		List<Transit> transits = null;
		List<Cabin> cabins = null;
		List<Agent> agents = null;
		List<StopOver> stopOvers = null;
		T planeInfo=null;
		if(null!=fcv && !fcv.isEmpty()){
			for(String family : fcv.keySet()){
				if(family.equals("planeinfo")){
					planeInfo=getSinglePlaneInfoByMap(fcv.get(family), refePrice,clz);
					if(null==planeInfo ){
						return null;
					}
				}
				if(family.equals("agentInfos")){
					agents = getAgentsByMap(fcv.get(family), refePrice);
				}
				if(family.equals("cabinsInfo")){
					cabins=getCabinsByMap(fcv.get(family), refePrice);
				}
				if(family.equals("stopOversInfo")){
					stopOvers = getStopOversByMap(fcv.get(family));
				}
				if(family.equals("transitsInfo")){
					transits = getTransitsByMap(fcv.get(family));
				}
				
			}
			if(null!=planeInfo){
				if(null!=agents && agents.size()>0){
					planeInfo.setAgents(agents);
					Agent agentEntity=null;
					for(Agent agent :agents){
						if(agent.getPrice()<=planeInfo.getTotalLowestPrice() && agent.getType()!=0 && agent.getPrice()>0){
							if(null == agentEntity){
								agentEntity=agent;
							}else if(agentEntity.getPrice()>=agent.getPrice()){
								agentEntity=agent;
							}
							
						}
					}
					if(null!=agentEntity && agentEntity.getPrice()>0){
						planeInfo.setAgentName(agentEntity.getName());
						planeInfo.setTotalLowestPrice(agentEntity.getPrice());
					}
				}
				if(null!=cabins && cabins.size()>0){
					planeInfo.setCabins(cabins);
				}
				if(null!=stopOvers && stopOvers.size()>0){
					planeInfo.setStopOvers(stopOvers);
					planeInfo.setStopOversType(stopOvers.size());
				}else{
					planeInfo.setStopOversType(0);
				}
				if(null!=transits && transits.size()>0){
					planeInfo.setTransits(transits);
					planeInfo.setTrainsType(transits.size()-1);
					String airport=null ;
					String time=null;
			
					int i=0;
					for(Transit vo:transits){
						
						i++;
						
						if(null!=vo.getToAirPort() && i<transits.size()){
							if(null==airport){
								airport=vo.getToAirPort();
							}else{
								airport+="-"+vo.getToAirPort();
							}
						}
						if(null!=vo.getStayTime() && vo.getStayTime()>=0 && i<transits.size() ){
							if(null==time){
								time=DateUtils.timeInMillis2Time(vo.getStayTime());
							}else{
								time+="-"+DateUtils.timeInMillis2Time(vo.getStayTime());
							}
						}
							
					}
					planeInfo.setTrainTime(time);
					planeInfo.setTrainsAirport(airport);

				}else{
					planeInfo.setStopOversType(0);
				}
			}
			
		}
		
		
		return planeInfo;
	}
	
	/**
	 * 将map 转换为planeinfo 对象 只有 planeinfo 主属性
	 * @param map Map<String, String> map
	 * @param refePrice 汇率
	 * @return
	 */
	private  static <T extends SinglePlaneInfo> T getSinglePlaneInfoByMap(Map<String, String> map,double refePrice,Class<T> clz) {
		 T  planeInfo =null;
		if(null!=map && !map.isEmpty()){
			try {
				planeInfo=clz.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			planeInfo.setSourceRowkey(map.get("SOURCE_ROWKEY"));
			planeInfo.setAreaCode(map.get("areaCode"));
			planeInfo.setAreaName(map.get("areaName"));
			planeInfo.setGrabChannelId(Long.parseLong(null==map.get("grabChannelId")? "0":map.get("grabChannelId")));
			planeInfo.setAttachHbaseKey(map.get("grabChannelNum"));
			planeInfo.setGrabDate(null==map.get("grabDate")? null:DateUtils.parse(map.get("grabDate"), "yyyyMMddHHmmssSSS"));
			planeInfo.setCreateTime(new Date());
			planeInfo.setGrabChannelName(map.get("grabChannel"));
			planeInfo.setCarrierKey(map.get("carrierKey"));
			planeInfo.setCarrierName(null==TestCommonData.getCarrierNameMap().get(planeInfo.getCarrierKey())? map.get("carrierName"):TestCommonData.getCarrierNameMap().get(planeInfo.getCarrierKey()));
			planeInfo.setCarrierFullName(null==TestCommonData.getCarrierFullNameMap().get(planeInfo.getCarrierKey()) ? map.get("carrierFullName") : TestCommonData.getCarrierFullNameMap().get(planeInfo.getCarrierKey()));
			planeInfo.setFlightNo(map.get("flightId"));
			planeInfo.setActuallyFlightNo(map.get("actuallyFlightNo"));
			planeInfo.setFlightType(map.get("type"));
			planeInfo.setFlightDate(map.get("flightBatchDate"));
			planeInfo.setStartTime(null==map.get("flightBatchStartTime")? null:DateUtils.parse(map.get("flightBatchStartTime").substring(0, 14), "yyyyMMddHHmmss"));
			if(null==planeInfo.getFlightDate() && null!=planeInfo.getStartTime()){
				planeInfo.setFlightDate(DateUtils.format(planeInfo.getStartTime(), "yyyy-MM-dd"));
			}
			planeInfo.setEndTime(null==map.get("flightBatchEndTime")? null:DateUtils.parse(map.get("flightBatchEndTime").substring(0, 14), "yyyyMMddHHmmss"));
			planeInfo.setFromCity(map.get("fromCity"));
			planeInfo.setFromCityName(map.get("fromCityName"));
			planeInfo.setToCity(map.get("toCity"));
			planeInfo.setToCityName(map.get("toCityName"));
			planeInfo.setTotalLowestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("lowerPrice")?"0":map.get("lowerPrice")),refePrice));
			planeInfo.setTotalHighestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("highPrice")?"0":map.get("highPrice")),refePrice));
			planeInfo.setAgentName(map.get("agentName"));
			planeInfo.setDepartureTime(null==planeInfo.getStartTime()?null:DateUtils.format(planeInfo.getStartTime(), "HH:mm"));
			planeInfo.setFlightDuration(Long.parseLong(null==map.get("flightDuration")? "0":map.get("flightDuration")));
			planeInfo.setSumHighestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("sumHighestPrice")?"0":map.get("sumHighestPrice")),refePrice));
			planeInfo.setSumLowestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("sumLowerPrice")?"0":map.get("sumLowerPrice")),refePrice));
			planeInfo.setTotalHighestTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("totalHighestTaxesPrice")?"0":map.get("totalHighestTaxesPrice")),refePrice));
			planeInfo.setTotalLowestTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("totalLowestTaxesPrice")?"0":map.get("totalLowestTaxesPrice")),refePrice));
			planeInfo.setCurrency(map.get("currency"));
		}
		return planeInfo;
	}

	/**
	 * 将Map对象转换为中转对象集合
	 * @param map
	 * @return
	 */
	private  static List<Transit> getTransitsByMap(Map<String, String> map) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("flightNo") || key.startsWith("price")){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<Transit> list =new ArrayList<Transit>();
		for(int i=0;i<max+1;i++){
			Transit entity =new Transit();
			entity.setCarrierKey(map.get("carrierKey_"+i));
			entity.setFlightNo(map.get("flightNo_"+i));
			String carrierkey=entity.getCarrierKey();
			if(null==carrierkey && entity.getFlightNo()!=null){
				carrierkey=entity.getFlightNo().substring(0, 2);
			}
			entity.setCarrierName(null==TestCommonData.getCarrierNameMap().get(carrierkey)? map.get("carrierName_"+i):TestCommonData.getCarrierNameMap().get(carrierkey));
			entity.setCarrierFullName(null==TestCommonData.getCarrierFullNameMap().get(carrierkey) ? map.get("carrierFullName_"+i) : TestCommonData.getCarrierFullNameMap().get(carrierkey));
			entity.setActuallyFlightNo(map.get("actuallyFlightNo_"+i));
			entity.setFlightType(map.get("flightType_"+i));
			entity.setFromAirPort(map.get("fromAirPort_"+i));
			entity.setFromAirPortName(map.get("fromAirPortName_"+i));
			entity.setToAirPort(map.get("toAirPort_"+i));
			entity.setToAirPortName(map.get("toAirPortName_"+i));
			entity.setStartTime(null!=map.get("startTime_"+i)? DateUtils.parse(map.get("startTime_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			entity.setEndTime(null!=map.get("endTime_"+i)? DateUtils.parse(map.get("endTime_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			entity.setStayTime(Long.parseLong(null!=map.get("stayTime_"+i)?map.get("stayTime_"+i):"0"));
			entity.setCabinName(map.get("cabinName_"+i));
			list.add(entity);
		}
		List<Transit> set= new ArrayList<Transit>();
		if(null!=list && list.size()>0){
			int i=0;
			for(Transit en :list){
				if(null!=en && null!=en.getFlightNo()){
					en.setSequence(i);
					i++;
					set.add(en);
				}
			}
		}
		return set;
	}

	/**
	 * 将Map对象转换为经停对象集合
	 * @param map
	 * @return
	 */
	private static  List<StopOver> getStopOversByMap(Map<String, String> map) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("stopCity") || key.startsWith("stayTime")){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<StopOver> list=new ArrayList<StopOver>();
		for(int i=0;i<max+1;i++){
			StopOver entity =new StopOver();
			entity.setStopCity(map.get("stopCity_"+i));
			entity.setStayTime(Long.parseLong(null!=map.get("stayTime_"+i)?map.get("stayTime_"+i):"0"));
			entity.setArrTime(null!=map.get("arrTime_"+i)? DateUtils.parse(map.get("arrTime_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			entity.setLeaveTime(null!=map.get("leaveTime_"+i)? DateUtils.parse(map.get("leaveTime_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			list.add(entity);
		}
		List<StopOver> set =new ArrayList<StopOver>();
		if(null!=list && list.size()>0){
			int i=1;
			for(StopOver en :list){
				if(null!=en &&  null!=en.getStopCity()){
					en.setSequence(i);
					i++;
					set.add(en);
				}
			}
		}
		return set;
	}

	/**
	 * 将Map对象转换为舱位对象集合
	 * @param map
	 * @param refePrice
	 * @return
	 */
	public  static List<Cabin> getCabinsByMap(Map<String, String> map,double refePrice) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("cabin") || key.startsWith("price")){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<Cabin> list=new ArrayList<Cabin>();
		for (int i = 0; i < max+1; i++) {
			Cabin entity =new Cabin();
			entity.setId(map.get("id_"+i));
			entity.setCabinType(map.get("cabinType_"+i));
			entity.setProductName(map.get("productName_"+i));
			entity.setSubCabinName(map.get("subCabinName_"+i));
			entity.setRebate(Double.parseDouble(map.get("rebate_"+i) != null ? map.get("rebate_"+i) : "0"));
			entity.setPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("price_"+i) != null ? map.get("price_"+i) : "0"),refePrice));
			entity.setOriginalPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("originalPrice_"+i) != null? map.get("originalPrice_"+i) : "0"),refePrice));
			entity.setBackCash(Double.parseDouble(map.get("backCash_"+i) != null? map.get("backCash_"+i) : "0"));
			entity.setMileageRatio(Double.parseDouble(map.get("mileageRatio_"+i) != null? map.get("mileageRatio_"+i) : "0"));
			entity.setTuigaiqian( map.get("tuigaiqian_"+i));
			entity.setZhijian(Double.parseDouble(map.get("zhijian_"+i) != null? map.get("zhijian_"+i) : "0"));
			entity.setLastSeatInfo( map.get("lastSeatInfo_"+i));
			entity.setTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("taxesPrice_"+i) != null? map.get("taxesPrice_"+i) : "0"),refePrice));
			list.add(entity);
		}
		List<Cabin> set =new ArrayList<Cabin>();
		if(null!=list && list.size()>0){
			for(Cabin en : list){
				if(null!=en && en.getPrice()>0){
					set.add(en);
				}
			}
		}
		return set;
	}
	
	/**
	 * 将map对象转换为代理信息集合
	 * @param map
	 * @param refePrice
	 * @return
	 */
	private static List<Agent> getAgentsByMap(Map<String, String> map,double refePrice) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("name") || key.startsWith("price")){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<Agent> list =new ArrayList<Agent>();
		for (int i = 0; i < max+1; i++) {
			Agent agentEntity =new Agent();
			agentEntity.setName(map.get("name_"+i));
			double price=Float.parseFloat(map.get("price_"+i) != null 
					 ? map.get("price_"+i) : "0");
			agentEntity.setPrice(PriceUtil.conversionPrice(price, refePrice));
			agentEntity.setOriginalPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("originalPrice_"+i) != null 
					 ? map.get("originalPrice_"+i) : "0"),refePrice));
			agentEntity.setAddress(map.get("address_"+i));
			agentEntity.setType(Integer.parseInt(map.get("agentType_"+i) != null 
					 ? map.get("agentType_"+i) : "9"));
			agentEntity.setCompanyName(map.get("companyName_"+i));
			agentEntity.setTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("taxesPrice_"+i) != null? map.get("taxesPrice_"+i) : "0"),refePrice));
			list.add(agentEntity);
		}
		List<Agent> set =new ArrayList<Agent>();
		if(null!=list && list.size()>0){
			for(Agent en : list){
				if(null!=en && null!=en.getPrice() && en.getPrice()>0){
					set.add(en);
				}
			}
		}
		return set;
	}
	
	/**
	 * 判断是否为数字    是 true
	 * @param str
	 * @return
	 */
   private  static boolean isNumeric(String str){
			return str.matches("\\d*");    
		 } 
   

   	
   /**
    * 
     * 将map集合转换为 DoublePlaneInfo 对象
	 * @param fcv Map<String, Map<String, String>>
	 * @param refePrice 汇率
	 * @return
    */
	public static <T extends DoublePlaneInfo> T hbaseToPlaneInfo(Map<String, Map<String, String>> fcv,double refePrice,Class<T> clz){
		T doublePlanInfo=null;
		List<ReturnDoublePlaneInfo> returnPlaneInfos =null;
		List<Transit> transits = null;
		List<Cabin> cabins = null;
		List<Agent> agents = null;
		List<StopOver> stopOvers = null;
		List<CabinRelation> cabinRelations =null;
		if(null!=fcv && !fcv.isEmpty()){
			for(String family : fcv.keySet()){
				if(family.equals("planeinfo")){
					doublePlanInfo=getOnlyPlaneInfoByMap(fcv.get(family), refePrice,clz);
					if(null==doublePlanInfo  ){
						return null;
					}
				}
				if(family.equals("agentInfos")){
					agents = getAgentsByMap(fcv.get(family), refePrice);
				}
				if(family.equals("cabinsInfo")){
					cabins=getCabinsByMap(fcv.get(family), refePrice);
				}
				if(family.equals("stopOversInfo")){
					stopOvers = getStopOversByMap(fcv.get(family));
				}
				if(family.equals("transitsInfo")){
					transits = getTransitsByMap(fcv.get(family));
				}
				if(family.equals("returnPlaneInfosInfo")){
					returnPlaneInfos=hbaseToReturnPlaneInfo(fcv,refePrice);
				}
				if(family.equals("cabinRelationsInfo")){
					cabinRelations=getCabinRelationsMap(fcv.get("cabinRelationsInfo"),refePrice);
				}
				
			}
			if(null!=doublePlanInfo){
				if(null!=agents && agents.size()>0){
					doublePlanInfo.setAgents(agents);
					Agent agentVO=null;
					for(Agent agent :agents){
						if(agent.getPrice()<=doublePlanInfo.getTotalLowestPrice() && agent.getType()!=0 && agent.getPrice()>0){
							if(null == agentVO){
								agentVO=agent;
							}else if(agentVO.getPrice()>agent.getPrice()){
								agentVO=agent;
							}
							
						}
					}
					if(null!=agentVO && agentVO.getPrice()>0){
						doublePlanInfo.setAgentName(agentVO.getName());
						doublePlanInfo.setTotalLowestPrice(agentVO.getPrice());
					}
				}
				if(null!=cabins && cabins.size()>0){
					doublePlanInfo.setCabins(cabins);
				}
				
				if(null!=stopOvers && stopOvers.size()>0){
					doublePlanInfo.setStopOvers(stopOvers);
					doublePlanInfo.setStopOversType(stopOvers.size());
				}else{
					doublePlanInfo.setStopOversType(0);
				}
				
				if(null!=transits && transits.size()>0){
					doublePlanInfo.setTransits(transits);
					doublePlanInfo.setTrainsType(transits.size()-1);
					String airport=null ;
					String time=null;
				
					int i=0;
					for(Transit vo:transits){
						
						i++;
						
						if(null!=vo.getToAirPort() && i<transits.size()){
							if(null==airport){
								airport=vo.getToAirPort();
							}else{
								airport+="-"+vo.getToAirPort();
							}
						}
						if(null!=vo.getStayTime() && vo.getStayTime()>=0 && i<transits.size() ){
							if(null==time){
								time=DateUtils.timeInMillis2Time(vo.getStayTime());
							}else{
								time+="-"+DateUtils.timeInMillis2Time(vo.getStayTime());
							}
						}
							
					}
					doublePlanInfo.setTrainTime(time);
					doublePlanInfo.setTrainsAirport(airport);
//					if(null!=startDate && null!=endDate){
//						doublePlanInfo.setTotalTime(DateUtils.dateDifference(startDate, endDate));
//					}
					
					
				}else{
					doublePlanInfo.setStopOversType(0);
				}
				
				if(null!=cabinRelations && cabinRelations.size()>0){
					doublePlanInfo.setCabinRelations(cabinRelations);
				}
				
				if(null!=returnPlaneInfos && returnPlaneInfos.size()>0){
					doublePlanInfo.setReturnPlaneInfos(returnPlaneInfos);
					if(null==doublePlanInfo.getReturnFlightDate()){
						for(ReturnDoublePlaneInfo vo :returnPlaneInfos){
							if(null!=vo){
								doublePlanInfo.setReturnFlightDate(vo.getFlightDate());
								if(null==vo.getFlightDate()&&null!=vo.getStartTime()){
									doublePlanInfo.setReturnFlightDate(DateUtils.format(vo.getStartTime(), "yyyy-MM-dd"));
								}
								if(null!=doublePlanInfo.getReturnFlightDate()){
									break;
								}
							}
						}
					}
				
				 /*  if(null== doublePlanInfo.getTotalLowestPrice() ||  doublePlanInfo.getTotalLowestPrice()==0){
					   for(ReturnDoublePlaneInfo vo :returnPlaneInfos){
						   if(null== doublePlanInfo.getTotalLowestPrice() ||  doublePlanInfo.getTotalLowestPrice()==0){
							   doublePlanInfo.setTotalLowestPrice(vo.getTotalLowestPrice());
						   }else if(doublePlanInfo.getTotalLowestPrice()>vo.getTotalLowestPrice()){
							   doublePlanInfo.setTotalLowestPrice(vo.getTotalLowestPrice());
						   }
					   }
				   }
				   if(null ==doublePlanInfo.getTotalHighestPrice() || doublePlanInfo.getTotalHighestPrice()==0){
					   for(ReturnDoublePlaneInfo vo :returnPlaneInfos){
						   if(null ==doublePlanInfo.getTotalHighestPrice() || doublePlanInfo.getTotalHighestPrice()==0){
							   doublePlanInfo.setTotalHighestPrice(vo.getTotalHighestPrice());
						   }else if(doublePlanInfo.getTotalHighestPrice()<vo.getTotalHighestPrice()){
							   doublePlanInfo.setTotalHighestPrice(vo.getTotalHighestPrice());
						   }
					   }
				   }*/
				}
				
			}
			
		}
		//价格为空的去年
//		if(doublePlanInfo.getTotalLowestPrice()==0){
//			return null;
//		}
		//没有返程的去掉
//		if(null==doublePlanInfo.getReturnPlaneInfos() || doublePlanInfo.getReturnPlaneInfos().isEmpty()){
//			return null;
//		}
		return doublePlanInfo;
	}
	
	/**
	 * 将map对象转换为 对象关系集合
	 * @param map
	 * @param refePrice
	 * @return
	 */
	private static List<CabinRelation> getCabinRelationsMap(Map<String, String> map,
			double refePrice) {
		int max = 0;
		for (String key: map.keySet()) {
			if(key.startsWith("cabinId") || key.startsWith("fullPrice")){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			  
			}
		}
		List<CabinRelation> list =new ArrayList<CabinRelation>();
		for (int i = 0; i < max+1; i++) {
			CabinRelation entity =new CabinRelation();
			entity.setCabinId(map.get("cabinId_"+i));
			entity.setFullPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("fullPrice_"+i) != null 
					 ? map.get("fullPrice_"+i) : "0"),refePrice));
			entity.setTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("taxesPrice_"+i) != null 
					 ? map.get("taxesPrice_"+i) : "0"),refePrice));
			entity.setTotalFullPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("totalFullPrice_"+i) != null 
					 ? map.get("totalFullPrice_"+i) : "0"),refePrice));
			entity.setReturnCabinId(map.get("returnCabinId_"+i));
			
			list.add(entity);
		}
		List<CabinRelation> newlist =new ArrayList<CabinRelation>();
		if(null!=list && list.size()>0){
			for(CabinRelation en:list){
				if(null!=en.getFullPrice() && en.getFullPrice()>0){
					newlist.add(en);
				}
			}
		}
		
		return newlist;
	}
	
	/**
	 * 
	 * @param fcv
	 * @param refePrice
	 * @return
	 */
	private static List<ReturnDoublePlaneInfo> hbaseToReturnPlaneInfo(
			Map<String, Map<String, String>> fcv, double refePrice) {
		int max=0;
		Map<String,String> map=fcv.get("returnPlaneInfosInfo");
		for(String key :map.keySet()){
			if(key.startsWith("flightId") || key.startsWith("lowerPrice")){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<ReturnDoublePlaneInfo> list=new ArrayList<ReturnDoublePlaneInfo>();
		for(int i=0;i<max+1;i++){
			ReturnDoublePlaneInfo returnPlanInfo=null;
			List<ReturnAgent> returnAgents = null;
			List<ReturnCabin> returnCabins=null;
			List<ReturnStopOver> returnStopOvers=null;
			List<ReturnTransit> returnTransits=null;
			returnPlanInfo=getReturnPlanInfoMap(map,i,refePrice);
			if(null==returnPlanInfo) continue;
			if(null!=fcv.get("returnAgentsInfo")){
				returnAgents=getReturnAgentsMap(fcv.get("returnAgentsInfo"),i,refePrice);
				returnPlanInfo.setReturnAgents(returnAgents);
				ReturnAgent agentVO=null;
				for(ReturnAgent agent :returnAgents){
					if(agent.getPrice()<=returnPlanInfo.getTotalLowestPrice() && agent.getAgentType()!=0 && agent.getPrice()>0){
						if(null == agentVO){
							agentVO=agent;
						}else if(agentVO.getPrice()>agent.getPrice()){
							agentVO=agent;
						}
						
					}
				}
				if(null!=agentVO && agentVO.getPrice()>0){
					returnPlanInfo.setAgentName(agentVO.getAgentName());
					returnPlanInfo.setTotalLowestPrice(agentVO.getPrice());
				}
			}
			if(null!=fcv.get("returnCabinsInfo")){
				returnCabins=getReturnCabinsMap(fcv.get("returnCabinsInfo"),i,refePrice);
				returnPlanInfo.setReturnCabins(returnCabins);
			}
			if(null!=fcv.get("returnStopOversInfo")){
				returnStopOvers=getReturnStopOversMap(fcv.get("returnStopOversInfo"),i);
				returnPlanInfo.setReturnStopOvers(returnStopOvers);
			}
			if(null!=fcv.get("returnTransitsInfo")){
				returnTransits=getReturnTransitsMap(fcv.get("returnTransitsInfo"),i);
				returnPlanInfo.setReturnTransits(returnTransits);
			}
			list.add(returnPlanInfo);
		}
		List<ReturnDoublePlaneInfo> newList = new ArrayList<ReturnDoublePlaneInfo>();
		if(null!=list && list.size()>0){
			for(ReturnDoublePlaneInfo en:list){
				if(null!=en && null!=en.getFlightNo()){
					newList.add(en);
				}
			}
		}
		return newList;
	}

	private static List<ReturnTransit> getReturnTransitsMap(Map<String, String> map,
			int j) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("flightNo_"+j) || key.startsWith("price_"+j)){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<ReturnTransit> list =new ArrayList<ReturnTransit>();
		for(int i=0;i<max+1;i++){
			ReturnTransit vo =new ReturnTransit();
			vo.setCarrierKey(map.get("carrierKey_"+j+"_"+i));
			vo.setCarrierName(map.get("carrierName_"+j+"_"+i));
			vo.setCarrierFullName(map.get("carrierFullName_"+j+"_"+i));
			vo.setFlightNo(map.get("flightNo_"+j+"_"+i));
			vo.setActuallyFlightNo(map.get("actuallyFlightNo_"+j+"_"+i));
			vo.setFlightType(map.get("flightType_"+j+"_"+i));
			vo.setFromAirPort(map.get("fromAirPort_"+j+"_"+i));
			vo.setFromAirPortName(map.get("fromAirPortName_"+j+"_"+i));
			vo.setToAirPort(map.get("toAirPort_"+j+"_"+i));
			vo.setToAirPortName(map.get("toAirPortName_"+j+"_"+i));
			vo.setStartTime(null!=map.get("startTime_"+j+"_"+i)? DateUtils.parse(map.get("startTime_"+j+"_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			vo.setEndTime(null!=map.get("endTime_"+j+"_"+i)? DateUtils.parse(map.get("endTime_"+j+"_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			vo.setStayTime(Long.parseLong(null!=map.get("stayTime_"+j+"_"+i)?map.get("stayTime_"+j+"_"+i):"0"));
			vo.setCabinName(map.get("cabinName_"+j+"_"+i));
			list.add(vo);
		}
		List<ReturnTransit> newList= new ArrayList<ReturnTransit>();
		if(null!=list && list.size()>0){
			for(ReturnTransit en :list){
				if(null!=en && null!=en.getFlightNo()){
					newList.add(en);
				}
			}
		}
		return newList;
	}

	private static List<ReturnStopOver> getReturnStopOversMap(
			Map<String, String> map, int j) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("stopCity_"+j) || key.startsWith("stayTime_"+j)){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<ReturnStopOver> list=new ArrayList<ReturnStopOver>();
		for(int i=0;i<max+1;i++){
			ReturnStopOver vo =new ReturnStopOver();
			vo.setStopCity(map.get("stopCity_"+j+"_"+i));
			vo.setStayTime(Long.parseLong(null!=map.get("stayTime_"+j+"_"+i)?map.get("stayTime_"+j+"_"+i):"0"));
			vo.setArrTime(null!=map.get("arrTime_"+j+"_"+i)? DateUtils.parse(map.get("arrTime_"+j+"_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			vo.setLeaveTime(null!=map.get("leaveTime_"+j+"_"+i)? DateUtils.parse(map.get("leaveTime_"+j+"_"+i).substring(0, 14), "yyyyMMddHHmmss") :null);
			list.add(vo);
		}
		List<ReturnStopOver> newList =new ArrayList<ReturnStopOver>();
		if(null!=list && list.size()>0){
			for(ReturnStopOver en :list){
				if(null!=en &&  null!=en.getStopCity()){
					newList.add(en);
				}
			}
		}
		return newList;
	}

	private static List<ReturnCabin> getReturnCabinsMap(Map<String, String> map,
			int j, double refePrice) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("cabin_"+j) || key.startsWith("price_"+j)){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<ReturnCabin> list=new ArrayList<ReturnCabin>();
		for (int i = 0; i < max+1; i++) {
			ReturnCabin vo =new ReturnCabin();
			vo.setCabinType(map.get("cabinType_"+j+"_"+i));
			vo.setProductName(map.get("productName_"+j+"_"+i));
			vo.setSubCabinName(map.get("subCabinName_"+j+"_"+i));
			vo.setRebate(Double.parseDouble(map.get("rebate_"+j+"_"+i) != null ? map.get("rebate_"+j+"_"+i) : "0"));
			vo.setPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("price_"+j+"_"+i) != null ? map.get("price_"+j+"_"+i) : "0"),refePrice));
			vo.setOriginalPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("originalPrice_"+j+"_"+i) != null? map.get("originalPrice_"+j+"_"+i) : "0"),refePrice));
			vo.setBackCash(Double.parseDouble(map.get("backCash_"+j+"_"+i) != null? map.get("backCash_"+j+"_"+i) : "0"));
			vo.setMileageRatio(Double.parseDouble(map.get("mileageRatio_"+j+"_"+i) != null? map.get("mileageRatio_"+j+"_"+i) : "0"));
			vo.setTuigaiqian( map.get("tuigaiqian_"+j+"_"+i));
			vo.setZhijian(Double.parseDouble(map.get("zhijian_"+j+"_"+i) != null? map.get("zhijian_"+j+"_"+i) : "0"));
			vo.setLastSeatInfo( map.get("lastSeatInfo_"+j+"_"+i));
			vo.setTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("taxesPrice_"+j+"_"+i) != null? map.get("taxesPrice_"+j+"_"+i) : "0"),refePrice));
			list.add(vo);
		}
		List<ReturnCabin> newList =new ArrayList<ReturnCabin>();
		if(null!=list && list.size()>0){
			for(ReturnCabin en : list){
				if(null!=en && en.getPrice()>0){
					newList.add(en);
				}
			}
		}
		return newList;
	}

	private static List<ReturnAgent> getReturnAgentsMap(Map<String, String> map,
			int j, double refePrice) {
		int max=0;
		for(String key :map.keySet()){
			if(key.startsWith("name_"+j) || key.startsWith("price_"+j)){
				String[] strs =key.split("_");
				if(!isNumeric(strs[strs.length-1])){
					continue;
				}
			  int num = Integer.parseInt(strs[strs.length-1]);
			  if(num > max){
				  max = num;
			  }
			}
		}
		List<ReturnAgent> list =new ArrayList<ReturnAgent>();
		for (int i = 0; i < max+1; i++) {
			ReturnAgent agentVO =new ReturnAgent();
			agentVO.setAgentName(map.get("name_"+j+"_"+i));
			double price=Float.parseFloat(map.get("price_"+j+"_"+i) != null 
					 ? map.get("price_"+j+"_"+i) : "0");
			agentVO.setPrice(PriceUtil.conversionPrice(price, refePrice));
			agentVO.setOriginalPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("originalPrice_"+j+"_"+i) != null 
					 ? map.get("originalPrice_"+j+"_"+i) : "0"),refePrice));
			agentVO.setAddress(map.get("address_"+j+"_"+i));
			agentVO.setAgentType(Integer.parseInt(map.get("agentType_"+j+"_"+i) != null 
					 ? map.get("agentType_"+j+"_"+i) : "9"));
			agentVO.setCompanyName(map.get("companyName_"+j+"_"+i));
			agentVO.setTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(map.get("taxesPrice_"+j+"_"+i) != null? map.get("taxesPrice_"+j+"_"+i) : "0"),refePrice));
			list.add(agentVO);
		}
		List<ReturnAgent> newList =new ArrayList<ReturnAgent>();
		for(ReturnAgent vo:list){
			if(null!=vo && null!=vo.getPrice() && vo.getPrice()>0){
				newList.add(vo);
			}
		}
		return newList;
	}

	private static ReturnDoublePlaneInfo getReturnPlanInfoMap(
			Map<String, String> map, int i, double refePrice) {
		ReturnDoublePlaneInfo planeInfo =new ReturnDoublePlaneInfo();
		if(null!=map && !map.isEmpty()){
			planeInfo.setAttachHbaseKey(map.get("grabChannelNum_"+i));
			planeInfo.setGrabDate(null==map.get("grabDate_"+i)? null:DateUtils.parse(map.get("grabDate_"+i).substring(0, 14), "yyyyMMddHHmmss"));
			planeInfo.setCreateTime(new Date());
			planeInfo.setGrabChannelId(Long.parseLong(null==map.get("grabChannelId_"+i)? "0":map.get("grabChannelId_"+i)));
			planeInfo.setGrabChannelName(map.get("grabChannel_"+i));
			planeInfo.setCarrierKey(map.get("carrierKey_"+i));
			planeInfo.setCarrierName(null==TestCommonData.getCarrierNameMap().get(planeInfo.getCarrierKey())? map.get("carrierName_"+i):TestCommonData.getCarrierNameMap().get(planeInfo.getCarrierKey()));
			planeInfo.setCarrierFullName(null==TestCommonData.getCarrierFullNameMap().get(planeInfo.getCarrierKey()) ? map.get("carrierFullName_"+i) : TestCommonData.getCarrierFullNameMap().get(planeInfo.getCarrierKey()));
			planeInfo.setFlightNo(map.get("flightId_"+i));
			planeInfo.setActuallyFlightNo(map.get("actuallyFlightNo_"+i));
			planeInfo.setFlightType(map.get("type_"+i));
			planeInfo.setFlightDate(null==map.get("flightBatchDate_"+i) ? null :map.get("flightBatchDate_"+i).substring(0,10));
			planeInfo.setStartTime(null==map.get("flightBatchStartTime_"+i)? null:DateUtils.parse(map.get("flightBatchStartTime_"+i).substring(0, 14), "yyyyMMddHHmmss"));
			if(null==planeInfo.getFlightDate() && null!=planeInfo.getStartTime()){
				planeInfo.setFlightDate(DateUtils.format(planeInfo.getStartTime(), "yyyy-MM-dd"));
			}
			planeInfo.setEndTime(null==map.get("flightBatchEndTime_"+i)? null:DateUtils.parse(map.get("flightBatchEndTime_"+i).substring(0, 14), "yyyyMMddHHmmss"));
			planeInfo.setFromCity(map.get("fromCity_"+i));
			planeInfo.setFromCityName(map.get("fromCityName_"+i));
			planeInfo.setToCity(map.get("toCity_"+i));
			planeInfo.setToCityName(map.get("toCityName_"+i));
			planeInfo.setTotalLowestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("lowerPrice_"+i)?"0":map.get("lowerPrice_"+i)), refePrice));
			planeInfo.setTotalHighestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("highPrice_"+i)?"0":map.get("highPrice_"+i)),refePrice));
			planeInfo.setAgentName(map.get("agentName_"+i));
			planeInfo.setSumHighestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("sumHighestPrice_"+i)?"0":map.get("sumHighestPrice_"+i)),refePrice));
			planeInfo.setSumLowestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("sumLowerPrice_"+i)?"0":map.get("sumLowerPrice_"+i)),refePrice));
			planeInfo.setTotalHighestTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("totalHighestTaxesPrice_"+i)?"0":map.get("totalHighestTaxesPrice_"+i)),refePrice));
			planeInfo.setTotalLowestTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("totalLowestTaxesPrice_"+i)?"0":map.get("totalLowestTaxesPrice_"+i)),refePrice));
			planeInfo.setFlightDuration(Long.parseLong(null==map.get("flightDuration_"+i)? "0":map.get("flightDuration_"+i)));
			
		}
		return planeInfo;
	}

	private static <T extends DoublePlaneInfo> T getOnlyPlaneInfoByMap(Map<String, String> map,
			double refePrice ,Class<T> clz) {
		T planeInfo =null;
		if(null!=map && !map.isEmpty()){
			try {
				planeInfo=clz.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			} 
			planeInfo.setAttachHbaseKey(map.get("grabChannelNum"));
			planeInfo.setGrabDate(null==map.get("grabDate")? null:DateUtils.parse(map.get("grabDate").substring(0, 14), "yyyyMMddHHmmss"));
			planeInfo.setCreateTime(new Date());
			planeInfo.setSourceRowkey(map.get("SOURCE_ROWKEY"));
			planeInfo.setAreaCode(map.get("areaCode"));
			planeInfo.setAreaName(map.get("areaName"));
			planeInfo.setGrabChannelId(Long.parseLong(null==map.get("grabChannelId")? "0":map.get("grabChannelId")));
			planeInfo.setGrabChannelName(map.get("grabChannel"));
			planeInfo.setCarrierKey(map.get("carrierKey"));
			planeInfo.setCarrierName(null==TestCommonData.getCarrierNameMap().get(planeInfo.getCarrierKey())? map.get("carrierName"):TestCommonData.getCarrierNameMap().get(planeInfo.getCarrierKey()));
			planeInfo.setCarrierFullName(null==TestCommonData.getCarrierFullNameMap().get(planeInfo.getCarrierKey()) ? map.get("carrierFullName") : TestCommonData.getCarrierFullNameMap().get(planeInfo.getCarrierKey()));
			planeInfo.setFlightNo(map.get("flightId"));
			planeInfo.setActuallyFlightNo(map.get("actuallyFlightNo"));
			planeInfo.setFlightType(map.get("type"));
			planeInfo.setFlightDate(null==map.get("flightBatchDate")? null :map.get("flightBatchDate").substring(0,10));
			planeInfo.setReturnFlightDate(map.get("flightReturnBatchDate"));
			planeInfo.setStartTime(null==map.get("flightBatchStartTime")? null:DateUtils.parse(map.get("flightBatchStartTime").substring(0, 14), "yyyyMMddHHmmss"));
			if(null==planeInfo.getFlightDate() && null!=planeInfo.getStartTime()){
				planeInfo.setFlightDate(DateUtils.format(planeInfo.getStartTime(), "yyyy-MM-dd"));
			}
			planeInfo.setEndTime(null==map.get("flightBatchEndTime")? null:DateUtils.parse(map.get("flightBatchEndTime").substring(0, 14), "yyyyMMddHHmmss"));
			planeInfo.setFromCity(map.get("fromCity"));
			planeInfo.setFromCityName(map.get("fromCityName"));
			planeInfo.setToCity(map.get("toCity"));
			planeInfo.setToCityName(map.get("toCityName"));
			planeInfo.setTotalLowestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("lowerPrice")?"0":map.get("lowerPrice")),refePrice));
			planeInfo.setTotalHighestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("highPrice")?"0":map.get("highPrice")), refePrice));
			planeInfo.setAgentName(map.get("agentName"));
			
			planeInfo.setFlightDuration(Long.parseLong(null==map.get("flightDuration")? "0":map.get("flightDuration")));
			planeInfo.setSumHighestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("sumHighestPrice")?"0":map.get("sumHighestPrice")),refePrice));
			planeInfo.setSumLowestPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("sumLowerPrice")?"0":map.get("sumLowerPrice")),refePrice));
			planeInfo.setTotalHighestTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("totalHighestTaxesPrice")?"0":map.get("totalHighestTaxesPrice")),refePrice));
			planeInfo.setTotalLowestTaxesPrice(PriceUtil.conversionPrice(Double.parseDouble(null==map.get("totalLowestTaxesPrice")?"0":map.get("totalLowestTaxesPrice")),refePrice));
			planeInfo.setCurrency(map.get("currency"));
		}
		return planeInfo;
	}

}
